  //格式化数字
const formatNumberDate = (n) => {
	n = n.toString()
	return n[1] ? n : '0' + n
}

//获取某月的天数
const getMonthDays = (year, myMonth) => {
	let monthStartDate = new Date(year, myMonth - 1, 1)
	let monthEndDate = new Date(year, myMonth, 1)
	let days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24)
	return days
}

//格式化日期
const formatDate = (date) => {
    let myyear = date.getFullYear()
    let mymonth = date.getMonth() + 1
    let myweekday = date.getDate()
    return [myyear, mymonth, myweekday].map(formatNumberDate).join('-')
}

//获取本月的开始日期
const getMonthStartDate = (year, month) => {
  console.log('dddddddddddd', year, month)
    return formatDate(new Date(year, month - 1, 1))
}
//获取本月的结束日期
const getMonthEndDate = (year, month) => {
	return formatDate(new Date(year, month - 1, getMonthDays(year, month)));
}

//获取当天及之前的时间
/**
* @author laoguo
* @param {Number} type 字符 
*	this.getXmonthToday(-1) //获取当前时间前1个月的日期 
*	this.getXmonthToday(-12) //获取当前时间前一年的日期
* 返回年月日
*/
const getXmonthToday = (type) => {
	// type 0 是当天 -1 是上个月   1是下个月
	var now = new Date(); // 可以传值调式 now = new Date(2019,2,30); 今天是3月30号
	var year = now.getFullYear(); //getYear()+1900=getFullYear()
	var month = now.getMonth() + 1; //0-11表示1-12月
	var day = now.getDate(); // 当天日期
	if (parseInt(month) < 10) {
		month = '0' + month;
	}
	if (parseInt(day) < 10) {
		day = '0' + day;
	}
	now = year + '-' + month + '-' + day; // 如果取当月日期可直接 return 返回

	let msum = Math.abs(type);

	// console.log(parseInt(msum / 12))

	var preMonth = 0; //月值

	let preSizes = 0; //上月总天数

	if (parseInt(msum / 12) === 1) {
		//一年
		// console.log("一整年")
		preMonth = parseInt(month);

		if (parseInt(month) === 12) {
			preSizes = new Date(year - 1, parseInt(month), 0).getDate();
			if (day > preSizes) {
				// console.warn(parseInt(year) - 1 + '-'+ preMonth +'-' + preSizes)
				return parseInt(year) - 1 + '-' + preMonth + '-' + preSizes;
			} else {
				// console.warn(parseInt(year) - 1 + '-'+ preMonth +'-' + day)
				return parseInt(year) - 1 + '-' + preMonth + '-' + day;
			}
		} else {
			// console.log("********")
			preSizes = new Date(year - 1, parseInt(month), 0).getDate();
			// console.log(preSizes)
			preMonth = preMonth < 10 ? '0' + preMonth : preMonth; // 获取上个月的值
			if (parseInt(day) > preSizes) {
				// console.warn(parseInt(year) - 1 + '-'+ preMonth +'-' + preSizes)
				return parseInt(year) - 1 + '-' + preMonth + '-' + preSizes;
			} else {
				// console.warn(parseInt(year) - 1 + '-'+ preMonth +'-' + day)
				return parseInt(year) - 1 + '-' + preMonth + '-' + day;
			}
		}
	} else if (parseInt(msum / 12) > 1) {
		//两年，三年
		// console.log("两年")

		preMonth = parseInt(month);

		if (parseInt(month) === 12) {
			// console.warn(preMonth)
			preSizes = new Date(year - 1, parseInt(month), 0).getDate();
			if (day > preSizes) {
				// console.warn(parseInt(year) - 1 + '-'+ preMonth +'-' + preSizes)
				return parseInt(year) - parseInt(msum / 12) + '-' + preMonth + '-' + preSizes;
			} else {
				// console.warn(parseInt(year) - 1 + '-'+ preMonth +'-' + day)
				return parseInt(year) - parseInt(msum / 12) + '-' + preMonth + '-' + day;
			}
		} else {
			// console.log("********")
			preSizes = new Date(year - parseInt(msum / 12), parseInt(month), 0).getDate();
			// console.log(preSizes)

			// console.log(Math.abs(msum) % 12)
			if (Math.abs(msum) % 12 === 0) {
				// console.log("整年")
				preMonth = preMonth < 10 ? '0' + preMonth : preMonth; // 获取上个月的值

				if (parseInt(day) > preSizes) {
					// console.warn(parseInt(year) - parseInt(msum / 12)  + '-'+ preMonth +'-' + preSizes)
					return parseInt(year) - parseInt(msum / 12) + '-' + preMonth + '-' + preSizes;
				} else {
					// console.warn(parseInt(year) - parseInt(msum / 12)  + '-'+ preMonth +'-' + day)
					return parseInt(year) - parseInt(msum / 12) + '-' + preMonth + '-' + day;
				}
			} else {
				// console.log(Math.abs(msum) % 12)
				// console.log("两年多")
				preMonth = parseInt(preMonth - (Math.abs(msum) % 12));

				preMonth = preMonth < 10 ? '0' + preMonth : preMonth; // 获取上个月的值
				if (parseInt(day) > preSizes) {
					// console.warn(parseInt(year) - parseInt(msum / 12)  + '-'+ preMonth +'-' + preSizes)
					return parseInt(year) - parseInt(msum / 12) + '-' + preMonth + '-' + preSizes;
				} else {
					// console.warn(parseInt(year) - parseInt(msum / 12)  + '-'+ preMonth +'-' + day)
					return parseInt(year) - parseInt(msum / 12) + '-' + preMonth + '-' + day;
				}
			}
		}

		// return parseInt(year) - parseInt(msum / 12) + '-'+ preMonth +'-' + preSizes;
	} else {
		//小于一年

		if (parseInt(month) === msum) {
			
			preMonth = parseInt(month) + 1;
			preMonth = preMonth < 10 ? '0' + preMonth : preMonth; // 获取上个月的值
			preSizes = new Date(year - 1, parseInt(month), 0).getDate();
			if (day > preSizes) {
				return parseInt(year) - 1 + '-' + preMonth + '-' + preSizes;
			} else {
				console.warn(parseInt(year) - 1 + '-' + preMonth + '-' + day);
				return parseInt(year) - 1 + '-' + preMonth + '-' + day;
			}
		} else {
			preSizes = new Date(year, parseInt(month), 0).getDate();
			
			if(parseInt(month) > msum){
				preMonth = parseInt(month) - msum;	
			}else if(parseInt(month) === msum){
				year = Number(year) - 1;
				preMonth = 12;
			}else{
				year = Number(year) - 1;
				preMonth = 12 - (msum - parseInt(month))
			}
			preMonth = preMonth < 10 ? '0' + preMonth : preMonth; // 获取上个月的值
			if (day > preSizes) {
				return parseInt(year) + '-' + preMonth + '-' + preSizes;
			} else {
				return parseInt(year) + '-' + preMonth + '-' + day;
			}
		}
	}
}

export {
	getMonthEndDate,getMonthStartDate,getXmonthToday
}